package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * Mybatis配置类：返回SqlSessionFactoryBean对象和MapperScannerConfigurer对象
 */
public class MybatisConfig {

    //返回SqlSessionFactoryBean对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dataSource);

        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.domain");

        return sqlSessionFactoryBean;
    }

    //返回MapperScannerConfigurer对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {

        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();

        //告诉Mybatis要扫描哪个包根据他来造动态代理对象
        mapperScannerConfigurer.setBasePackage("com.itheima.dao");

        return mapperScannerConfigurer;
    }
}
